package com.example.administrator.familfinalcialmanagement.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.CommonUser;
import com.example.administrator.familfinalcialmanagement.databinding.ItemCommonDescBinding;
import com.example.administrator.familfinalcialmanagement.util.listener.BaseRecyclerViewHolder;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.List;

public class CommonUserAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<CommonUser> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public CommonUserAdapter(Context context, List<CommonUser> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemCommonDescBinding functionBinding = DataBindingUtil.inflate( LayoutInflater.from( mcontext ),
                R.layout.item_common_desc, parent, false );
        return new TourViewHolder(functionBinding);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData( strings.get( position ) );
        TourViewHolder tv = (TourViewHolder) holder;
        if (!TextUtils.isEmpty( strings.get( position ).getOne() )) {

            switch (strings.get( position ).getOne()) {
                case "JAVA":
                    tv.llModuleOne.setVisibility( View.VISIBLE );
                    tv.llModuleThree.setVisibility( View.GONE );
                    tv.llModuleTwo.setVisibility( View.GONE );

                    tv.llModuleOneNum.setVisibility( View.VISIBLE );
                    tv.llModuleTwoNum.setVisibility( View.GONE );
                    tv.llModuleThreeNum.setVisibility( View.GONE );
                    break;
                case "C":
                    tv.llModuleOne.setVisibility( View.GONE );
                    tv.llModuleThree.setVisibility( View.VISIBLE );
                    tv.llModuleTwo.setVisibility( View.GONE );

                    tv.llModuleOneNum.setVisibility( View.GONE );
                    tv.llModuleTwoNum.setVisibility( View.VISIBLE );
                    tv.llModuleThreeNum.setVisibility( View.GONE );

                    break;
                case "PHP":
                    tv.llModuleOne.setVisibility( View.GONE );
                    tv.llModuleThree.setVisibility( View.GONE );
                    tv.llModuleTwo.setVisibility( View.VISIBLE );

                    tv.llModuleOneNum.setVisibility( View.GONE );
                    tv.llModuleTwoNum.setVisibility( View.GONE );
                    tv.llModuleThreeNum.setVisibility( View.VISIBLE );
                    break;
                default:
            }
        }

        if (strings.get(position).getPwdStatus().equals("1")) {
            tv.tvModulePwdStatus.setText("锁定");
        }else{
            tv.tvModulePwdStatus.setText("非锁定");
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick( holder.itemView, pos );
                }
            } );

            holder.itemView.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick( holder.itemView, pos );
                    return false;
                }
            } );
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<CommonUser> {
        ItemCommonDescBinding functionBinding;
        LinearLayout llModuleOne, llModuleTwo, llModuleThree, llModuleOneNum, llModuleTwoNum, llModuleThreeNum;
        TextView tvModulePwdStatus;

        public TourViewHolder(ItemCommonDescBinding dataBinding) {
            super( dataBinding.getRoot() );
            functionBinding = dataBinding;
            llModuleOne = dataBinding.llModuleOne;
            llModuleTwo = dataBinding.llModuleTwo;
            llModuleThree = dataBinding.llModuleThree;
            llModuleOneNum = dataBinding.llModuleOneNum;
            llModuleTwoNum = dataBinding.llModuleTwoNum;
            llModuleThreeNum = dataBinding.llModuleThreeNum;
            tvModulePwdStatus = dataBinding.tvModulePwdStatus;
        }

        @Override
        public void bindData(CommonUser data) {
            functionBinding.setBean( data );
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }
}
